﻿using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace InsPipeInfo
{
    public class InfObj
    {
        public InfObj(IRow row)
        {
            int i = row.RowNum + 1;
            int tempI = 0;
            double tempD = 0.0;
            string defalutString = "--";

            //流水号
            if (!int.TryParse(row.GetCell(0).ToString(), out tempI) || tempI == 0)
            {
                throw new Exception(string.Format("第 {0} 行的 流水号 格式不正确", i));

            }
            NUMBER = tempI.ToString().PadLeft(3, '0');
            //管径
            if (!Double.TryParse(row.GetCell(1).ToString(), out tempD) || tempD == 0)
            {
                throw new Exception(string.Format("第 {0} 行的 管径号 格式不正确", i));

            }
            NOMINAL_DIAMETER = tempD.ToString();
            //传感器标识
            if (row.GetCell(2) == null)
            {
                throw new Exception(string.Format("第 {0} 行的 传感器标识 不能为空", i));
            }
            INST_HPR = row.GetCell(2).ToString().ToUpper();
            //仪表架编号
            if (!int.TryParse(row.GetCell(3).ToString(), out tempI))
            {
                throw new Exception(string.Format("第 {0} 行的 仪表架编号 只能输入数字", i));
            }
            if (tempI == 0)
            {
                INST_FRM_CODE_HPR = "--";
            }
            else
            {
                INST_FRM_CODE_HPR = INST_HPR.Substring(0, 4) + tempI.ToString().PadLeft(3, '0') + "CQ";
            }

            //焊口 
            WELD_HPR = row.GetCell(4).ToString() == "T" ? "TRUE" : "FALSE";
            // 伴热 
            TRACING = row.GetCell(5).ToString() == "T" ? "TRUE" : "FALSE";
            //房间号 
            if (row.GetCell(6) == null)
            {
                throw new Exception(string.Format("第 {0} 行的 房间号 不能为空", i));
            }
            ROOM_HPR = row.GetCell(6).ToString();

            //仪表架标准 
            INST_FRM_STD_HPR = row.GetCell(7) == null ? defalutString : row.GetCell(7).ToString();
            //流体类型  
            FLUID_HPR = row.GetCell(8) == null ? defalutString : row.GetCell(8).ToString();
            //保温 
            INSULATION_THICKNESS = row.GetCell(9).ToString() == "T" ? "40" : "0";
            //保护组/ 列  

            DIVISION_HPR = row.GetCell(10) == null ? defalutString : row.GetCell(10).ToString();
            // 工艺设备 
            EQUIP_HPR = row.GetCell(11) == null ? defalutString : row.GetCell(11).ToString();
            //试验方法

            if (!int.TryParse(row.GetCell(12).ToString(), out tempI))
            {
                throw new Exception(string.Format("第 {0} 行的 试验方式 格式不正确", i));

            }
            if (tempI == 1)
            {
                TEST_METHOD_HPR = "水压";
            }
            else if (tempI == 2)
            {
                TEST_METHOD_HPR = "气压";
            }
            else
            {
                TEST_METHOD_HPR = "无需试验";
            }

            //设计压力 
            if (!Double.TryParse(row.GetCell(13).ToString(), out tempD))
            {
                throw new Exception(string.Format("第 {0} 行的 设计压力  必须是数字", i));
            }
            DESIGN_PRESSURE = tempD.ToString();
            //传感器标准 
            SENSOR_CODE_HPR = row.GetCell(14) == null ? defalutString : row.GetCell(14).ToString();
            //试压压力 
            if (!Double.TryParse(row.GetCell(15).ToString(), out tempD))
            {
                throw new Exception(string.Format("第 {0} 行的 试压压力   必须是数字", i));
            }
            TEST_PRESSURE = tempD.ToString();
            //厂房  
            if (row.GetCell(16).ToString().Length != 2)
            {
                throw new Exception(string.Format("第 {0} 行的 厂房    不正确", i));
            }
            FACTORY_HPR = row.GetCell(16).ToString();
            //区域 
            if (row.GetCell(17).ToString().Length != 3)
            {
                throw new Exception(string.Format("第 {0} 行的 区域    不正确", i));
            }
            AREA_HPR = row.GetCell(17).ToString();

            //图纸号 
            if (row.GetCell(18) == null)
            {
                throw new Exception(string.Format("第 {0} 行的 图纸号 不能为空", i));
            }
            DRAWING_NUMBER_HPR = row.GetCell(18).ToString();
            //图纸名称 

            DRAWING_NAME_HPR = INST_HPR + "仪表管道ISO图";
            //安全等级  
            if (row.GetCell(19) == null)
            {
                throw new Exception(string.Format("第 {0} 行的 安全等级 不能为空", i));

            }
            SECURITY_LEVEL_HPR = row.GetCell(19).ToString();
            //质保级别 
            if (row.GetCell(20) == null)
            {
                throw new Exception(string.Format("第 {0} 行的 安全等级 不能为空", i));

            }
            WARRANTY_LEVEL_HPR = row.GetCell(20).ToString();

            //描述

            DESCRIPTION = row.GetCell(21) == null ? defalutString : row.GetCell(21).ToString();

        }

        /// <summary>
        /// 描述
        /// </summary>
        public string DESCRIPTION { get; set; }

        public string DESIGN_PRESSURE { get; set; }

        public string INSULATION_THICKNESS { get; set; }

        public string NOMINAL_DIAMETER { get; set; }

        public string NUMBER { get; set; }

        public string SPECIFICATION { get; set; } = "RCCM";

        public string TEST_PRESSURE { get; set; }

        public string TRACING { get; set; }

        public string DIVISION_HPR { get; set; }

        public string FACTORY_HPR { get; set; }

        public string INST_HPR { get; set; }

        public string SENSOR_CODE_HPR { get; set; }

        public string ROOM_HPR { get; set; }

        public string EQUIP_HPR { get; set; }

        public string WELD_HPR { get; set; }

        public string FLUID_HPR { get; set; }

        public string AREA_HPR { get; set; }

        public string TEST_METHOD_HPR { get; set; }

        public string DRAWING_NUMBER_HPR { get; set; }

        public string DRAWING_NAME_HPR { get; set; }

        public string INST_FRM_CODE_HPR { get; set; }

        public string INST_FRM_STD_HPR { get; set; }

        public string SECURITY_LEVEL_HPR { get; set; }

        public string WARRANTY_LEVEL_HPR { get; set; }

    }
    public class InfGroup
    {
        public InfGroup(string service)
        {
            SERVICE = service;
        }


        public void Add(InfObj infObj)
        {
            if (!ObjList.Contains(infObj))
            {
                ObjList.Add(infObj);
            }
        }
        public string SERVICE { get; }

        private List<InfObj> ObjList = new List<InfObj>();
        public void Write()
        {
            Console.WriteLine(SERVICE + "系统");
            Console.WriteLine("");
            string template = "{0}={1}";
            StringBuilder sb = new StringBuilder();


            sb.AppendLine("[Associations]");
            Console.WriteLine("[Associations]");
            sb.AppendLine(string.Format(template, "SERVICE", SERVICE));
            Console.WriteLine(string.Format(template, "SERVICE", SERVICE));
            sb.AppendLine("UNIT=" + "KK2");
            Console.WriteLine("UNIT=" + "KK2");
            sb.AppendLine("");
            Console.WriteLine("");

            foreach (InfObj item in ObjList)
            {

                sb.AppendLine("[InstanceData]");
                Console.WriteLine("[InstanceData]");
                foreach (PropertyInfo p in item.GetType().GetProperties())
                {

                    string temp = string.Format(template, p.Name, p.GetValue(item));
                    Console.WriteLine(temp);
                    sb.AppendLine(temp);
                }
                sb.AppendLine();
                Console.WriteLine("");
            }
            File.WriteAllText(SERVICE + ".txt", sb.ToString(), Encoding.UTF8);
        }
    }

}

